package computer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;


public class Calculator extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTextField tf;
	private String prepo="+";
	private double s=0;
	private boolean isnew=true;
	private boolean islx=false;
	
	
	
	public Calculator(){
		
	
		setVisible(true);
		setSize(500, 300);
		setLocation(400, 200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Font fon=new Font("行楷", Font.PLAIN, 50);
		
		JPanel top=new JPanel();
		top.setLayout(new BorderLayout());
		tf=new JTextField("0");
		tf.setDisabledTextColor(Color.BLUE);
		tf.setFont(fon);
		tf.setHorizontalAlignment(JTextField.RIGHT);
		tf.setEnabled(false);
		
		JButton key=new JButton("清除");
		key.setFont(fon);
		key.setBackground(new Color(0xFF6666));
		key.addActionListener(this);
		
		top.add(tf);top.add(key,BorderLayout.EAST);
		
		
		
		JPanel centre=new JPanel();
		centre.setLayout(new GridLayout(4, 4));
		String[] num={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
		for (String s : num) {
			JButton bt=new JButton(s);
			bt.setFont(fon);
			bt.setBackground(new Color(0xFF6666));
			bt.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			bt.addActionListener(this);
			centre.add(bt);
			
		}
		add(top,BorderLayout.NORTH);add(centre);
		
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Calculator();
				
				
				
			}
			
	});
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd=e.getActionCommand();
		if("0.123456789".indexOf(cmd)>=0){
			showNum(cmd);
		}
		else if(cmd.equals("清除")){
			init();			
		}
		
		
		else calculator(cmd);
		
	}
	private void init() {
		tf.setText("0");
		s=0;
		prepo="+";
		isnew=true;
		islx=false;
	}
	private void calculator(String cmd) {
		isnew=true;
		if(islx){prepo=cmd;return;}
		double oc=Double.valueOf(tf.getText());
		switch(prepo){
		case "+":s+=oc;break;
		case "-":s-=oc;break;
		case "*":s*=oc;break;
		case "/":s/=oc;break;
		}
		prepo=cmd;
		String str=String.valueOf(s);
		if(str.endsWith(".0")) str=str.substring(0, str.length()-2);
		if(str.equals("Infinity")) str="除数不能为0";
		tf.setText(str);
		islx=true;
	}
	
	private void showNum(String cmd) {
		if(prepo.equals("=")) init();
		String or=tf.getText();
		islx=false;
		if(isnew==true){
			or="0";
			isnew=false;
			
		}
		if(or.indexOf(".")>=0&&or.equals("."))return;
		if(or.equals("0")){
			if(cmd.equals("."))tf.setText("0.");
			else tf.setText(cmd);
		}
		else tf.setText(or+cmd);
	}


}
